<template>
  <view class="content u-f-ajc">
    <view class="floatimg ">
      <image src="/static/index/index_left.png" mode="widthFix"></image>
      <view class="right_box u-f-ajc">
        <view>
          <h3>臻学派</h3>
          <view class="regbtn btn" @click="registerFace">
            注册
          </view>
          <view class="login btn" @click="login">登录</view>

        </view>

      </view>
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations
  } from 'vuex'
  export default {
    computed: {
      ...mapState(['userInfo'])
    },
    data() {
      return {}
    },
   async onLoad() {
      // let obj = await this.$utils.GetMacAdress()
      // console.log(obj.uuid)
      // 检查视频文件是否存在
      // this.$utils.resolveLocalFileSystemURL('/storage/emulated/0/videoTopic').then(entry=>{
        if(this.userInfo){
          // 进入首页
          this.$redirectTo('/pages/index/home')
        }
      // }).catch(err=>{
      //   this.$toPage('/pages/index/subject/copy')
      // })
       // var reader = null;
       // entry.file(  ( file )=> {
       // 	reader = new plus.io.FileReader();
       // 	reader.onloadend =  ( e )=> {
       // 		console.log( "Read success" );
       // 		// Get data
       // 		console.log( e.target.result );
       //    if(e.target.result==='1234'){
       //      this.$toPage('/pages/index/subject/copy')
             
       //    }
       // 	};
       // 	reader.readAsText( file );
       // },  ( e ) =>{
       // console.log( e.message );
       // } );
       
       // // ......
       // // Abort read data from file
       // return false
      
    
    },
    methods: {
      ...mapMutations(['setUserInfo', 'setFaceBaceUrl']),
      // 登录
      async login() {
        let obj = await this.$utils.GetMacAdress()
        let macAddress = obj.uuid
        let baseStr = await this.$utils.faceLicense(2)
        let flag = await this.IsRegister(baseStr)
        //  let baseStr = ""
        // let flag=true
        if (flag) {
          let res = await this.$http.post('/face/login', {
            macAddress: macAddress,
            baseStr: baseStr
          })
          if (res.status === 0) {
            this.setFaceBaceUrl('data:image/jpg;base64,' + baseStr)
            this.$setStorage('token', res.data.token)
            this.$setStorage('user', JSON.stringify(res.data.student))
            this.setUserInfo(res.data.student)
            // 进入首页
            this.$redirectTo('/pages/index/home')
          } else {
            this.$showToast(res.msg)
          }
        } else {
          this.$showToast('未注册')
        }
      },
      // 检测是否注册
      async IsRegister(baseStr) {
        let res = await this.$http.post('/face/search', {
          baseStr: baseStr
        });
        // 0是未注册，1是已注册
        if (res.status === 0) {
          return false;

        } else {
          return true;

        }
      },
      // 点击注册按钮
      async registerFace() {
        let baseStr = await this.$utils.faceLicense(2)
        let flag = await this.IsRegister(baseStr)
        if (flag) {
          this.$showToast('已注册，请点击登录')
        } else {
          this.setFaceBaceUrl('data:image/jpg;base64,' + baseStr)
          this.$redirectTo('/pages/login/register')
        }



      }

    }
  }
</script>

<style lang="scss">
  .content {
    height: 100%;
    background: url(../../static/index/index_bg.png) no-repeat;
    background-size: cover;

    .fullimg {
      width: 100%;
      height: 100%;
    }

    .floatimg {
      position: relative;
      width: px2vw(1192);
      height: px2vh(825);

      image {
        width: 100%;
        height: 100%;
      }

      .right_box {
        position: absolute;
        right: 0px;
        top: 0px;
        width: 50%;
        height: 100%;

        h3 {
          height: px2vh(70);
          font-size: px2vw(50);
          font-family: PingFangSC-Medium, PingFang SC;
          font-weight: 500;
          color: $global-color;
          line-height: px2vh(70);
          text-align: center;
        }

        .regbtn {
          background: #FFFFFF;
          box-shadow: 0px 1px 6px 0px rgba(61, 159, 234, 0.3);
          border-radius: px2vh(32);
          color: $global-color;
           margin: px2vh(90) auto px2vw(30);
        }

        .btn {
          height: px2vh(54);
          line-height: px2vh(54);
        }

        .login {
          width: px2vw(250);
          background: $global-color;
        }
      }
    }
  }
</style>
